package com.zhanjiangyifang.shop.base;

import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.zhanjiangyifang.shop.R;
import com.zhanjiangyifang.shop.utils.ToastUtils;
import com.zhanjiangyifang.shop.widget.StatueLayout;
import com.zhanjiangyifang.shop.widget.sweetdialog.SweetAlertDialog;

import butterknife.ButterKnife;

/**
 * Created by admin on 2018/7/5.
 */

public abstract class BaseActivity <T extends BasePresenter> extends AppCompatActivity implements BaseView{
    protected T basePresenter;

    private SweetAlertDialog dialog;
    private StatueLayout statueLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (toggleOverridePendingTransition()) {
            overridePendingTransition(R.anim.right_in, R.anim.left_out);
        }
        basePresenter = createPresenter();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        statueLayout=setStatueLayout();
        initView();
    }

    //-------------------抽象方法 子类必须实现--------------------
    /**获取子布局*/
    public abstract int getLayoutId();

    /**子类传状态控件到父类统一处理*/
    protected abstract StatueLayout setStatueLayout();

    /**初始化presenter @return*/
    protected abstract T createPresenter();

    /**初始化view*/
    protected abstract void initView();

    /*-------------------------加载进度框---------------------*/
    @Override
    public void showLoadingDialog() {
        if (dialog == null) {
            dialog = new SweetAlertDialog(this);
        }
        if (!isFinishing() && !dialog.isShowing()) {
            dialog.show();
        }
    }

    @Override
    public void showLoadingDialog(String message) {
        if (dialog == null) {
            dialog = new SweetAlertDialog(this);
            dialog.setTitleText(message);
        }
        if (!isFinishing() && !dialog.isShowing()) {
            dialog.show();
        }
    }

    @Override
    public void dismissLoadingDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    /*-------------------------Toast---------------------*/
    @Override
    public void showToast(String msg) {
        ToastUtils.showShort(this, msg);
    }

    @Override
    public void showLongToast(String msg) {
        ToastUtils.showLong(this, msg);
    }


    /*-------------------------自定义错误页，空页展示方法---------------------*/
    private boolean isStatueLayoutEmpty(){
        return statueLayout!=null;
    }

    @Override
    public void showError() {
        if (isStatueLayoutEmpty()) {
            statueLayout.showError(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showLoading();
                    initView();
                }
            });
        }
    }

    @Override
    public void showError(int resId, String text) {
        if (isStatueLayoutEmpty()) {
            statueLayout.showError(resId, text, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showLoading();
                    initView();
                }
            });
        }
    }

    @Override
    public void showEmpty() {
        if (isStatueLayoutEmpty()) {
            statueLayout.showEmpty();
        }
    }

    @Override
    public void showEmpty(String text) {
        if (isStatueLayoutEmpty()) {
            statueLayout.showEmpty(text);
        }
    }

    @Override
    public void showEmpty(int resId, String text) {
        if (isStatueLayoutEmpty()) {
            statueLayout.showEmpty(resId,text);
        }
    }

    @Override
    public void showLoading() {
        if (isStatueLayoutEmpty()) {
            statueLayout.showLoading();
        }
    }

    @Override
    public void hideStatueView() {
        if (isStatueLayoutEmpty()) {
            statueLayout.hide();
        }
    }

    /*--------------------页面进入、退出样式----------------------*/
    /**
     * 是否支持页面跳转动画
     * @return
     */
    protected boolean toggleOverridePendingTransition(){
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dialog = null;
    }

    @Override
    public void finish() {
        super.finish();
        if (toggleOverridePendingTransition()) {
            overridePendingTransition(R.anim.left_in, R.anim.right_out);
        }
    }

    /**
     * 设置沉浸状态栏与导航栏
     */
    public void setStatusBar(boolean state){
        Window window = getWindow();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT != Build.VERSION_CODES.LOLLIPOP) {
            if(state){
                //透明导航栏
                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            }
            //透明状态栏
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS |
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    // | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }
    }
}
